package com.lm.jsqlparser.example;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;

public class ModifySQLCondition {

    public static void main(String[] args) throws JSQLParserException {
        String sql = "SELECT * FROM orders WHERE status = 'completed'";

        // 解析 SQL
        Select selectStatement = (Select) CCJSqlParserUtil.parse(sql);
        PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();

        // 创建新的条件
        Expression newCondition = CCJSqlParserUtil.parseCondExpression("amount > 100");

        // 修改 WHERE 条件（AND 连接）
        if (plainSelect.getWhere() != null) {
            plainSelect.setWhere(CCJSqlParserUtil.parseCondExpression(
                    plainSelect.getWhere() + " AND " + newCondition));
        } else {
            plainSelect.setWhere(newCondition);
        }

        // 输出修改后的 SQL
        System.out.println("修改后的 SQL：" + selectStatement);
    }
}

